package cn.blunce.timebroadcast;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.Calendar;


public class MainActivity extends Activity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    public final static String IS_VIBRATE = "is_vibrate";
    public final static String IS_VOICE = "is_voice";

    private EditText ET_running_time;
    private EditText ET_time_interval;
    private RadioGroup RG_isvivrator;
    private Button btn_apply;
    private Button btn_cancel;

    private float running_time;
    private float time_interval;
    private boolean isVibrator = true;

    public static AlarmManager alarmManager;
    private Intent intent_set_alarm;
    private Intent intent_cancel_alarm;
    public static PendingIntent setAlarmIntent;
    private PendingIntent cancelAlarmIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initParameter();
    }

    private void initParameter() {
        intent_set_alarm = new Intent(this, AlarmIntentService.class);
        intent_cancel_alarm = new Intent(this, CancelAlarmIntentService.class);
        alarmManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);

        ET_running_time = (EditText) findViewById(R.id.runing_time);
        ET_time_interval = (EditText) findViewById(R.id.time_interval);
        RG_isvivrator = (RadioGroup) findViewById(R.id.is_vibrator);
        btn_apply = (Button) findViewById(R.id.btn_apply);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);

        RG_isvivrator.setOnCheckedChangeListener(this);
        btn_apply.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);

        readSetting();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_apply:
                buttonApply();
                break;
            case R.id.btn_cancel:
                buttonCancel();
                break;
            default:
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.yes)
            isVibrator = true;
        else
            isVibrator = false;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void buttonApply() {
        saveSetting();

        intent_set_alarm.putExtra(IS_VIBRATE, isVibrator);
        intent_set_alarm.putExtra(IS_VOICE, false);
        setAlarmIntent = PendingIntent.getService(this, 0, intent_set_alarm, 0);
        alarmManager.cancel(setAlarmIntent);
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, (int) (time_interval * 60) * 1000l, (int) (time_interval * 60) * 1000l, setAlarmIntent);

        cancelAlarmIntent = PendingIntent.getService(this, 22, intent_cancel_alarm, 0);
        alarmManager.setExact(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis() + (int) (running_time * 3600) * 1000l, cancelAlarmIntent);
    }

    private void buttonCancel() {
        alarmManager.cancel(setAlarmIntent);
        alarmManager.cancel(cancelAlarmIntent);
        Log.i("AlarmTest", "Cancel all alarms!!");
    }


    private void saveSetting() {
        SharedPreferences sharedPreferences = this.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat("ET_running_time", running_time = Float.parseFloat(ET_running_time.getText().toString()));
        editor.putFloat("ET_time_interval", time_interval = Float.parseFloat(ET_time_interval.getText().toString()));
        editor.putBoolean("isVibrator", isVibrator);
        editor.commit();
        Toast.makeText(this, "Setting Saved!!", Toast.LENGTH_SHORT).show();
    }

    private void readSetting() {
        SharedPreferences sharedPreferences = this.getPreferences(Context.MODE_PRIVATE);
        ET_running_time.setText(String.valueOf(running_time = sharedPreferences.getFloat("ET_running_time", 0)));
        ET_time_interval.setText(String.valueOf(time_interval = sharedPreferences.getFloat("ET_time_interval", 0)));
        if (isVibrator = sharedPreferences.getBoolean("isVibrator", true))
            ((RadioButton) findViewById(R.id.yes)).setChecked(true);
        else
            ((RadioButton) findViewById(R.id.no)).setChecked(true);
    }

}
